/*
 * Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
 *
 * Licensed under the Mulan PSL v2.
 * You can use this file according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of the License at
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
 * FOR MORE DETAILS.
 */

#pragma once

#include "highfive/H5File.hpp"

#include "DBManager/IRepository/Input/Property/IPCOMPRepository.h"
#include "DBManager/Repository/HDF5/Base/MainlyRepositoryBase.h"
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
namespace PCOMP
{
    struct IDENTITY
    {
        SG::DataStructure::Common::I64  PID;      // Property identification number
        SG::DataStructure::Common::I64  NPLIES;   // Number of plies, negative if symmetric
        SG::DataStructure::Common::Real Z0;       // Distance from the reference plane to the bottom surface
        SG::DataStructure::Common::Real NSM;      // Nonstructural mass per unit area
        SG::DataStructure::Common::Real SB;       // Allowable shear stress of the bonding material
        SG::DataStructure::Common::I64  FT;       // Failure Theory: 0=blank, 1=HILL, 2=HOFF, 3=TSAI, 4=STRN
        SG::DataStructure::Common::Real TREF;     // Reference temperature
        SG::DataStructure::Common::Real GE;       // Damping coefficient
        SG::DataStructure::Common::I64  PLY_POS;  // Start position of PLY data in the PLY dataset
        SG::DataStructure::Common::I64  PLY_LEN;  // Length of PLY data in the PLY dataset
        SG::DataStructure::Common::I64  DOMAIN_ID;
    };
};  // namespace PCOMP
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::PCOMP::IDENTITY> ()
{
    HighFive::CompoundType compoundType = {
        { "PID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "NPLIES", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "Z0", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "NSM", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "SB", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "FT", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "TREF", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "GE", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "PLY_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "PLY_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "DOMAIN_ID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
    };
    return compoundType;
}
template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::ShellSectionLayer> ()
{
    HighFive::CompoundType compoundType = {
        { "MID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "T", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "THETA", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "SOUT", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
    };
    return compoundType;
}
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
class PCOMPRepository : public IPCOMPRepository, public MainlyRepositoryBase<SG::DataStructure::FEM::PCOMPData>
{
  public:
    explicit PCOMPRepository (HDF5Database& database);

    enum DATASET : std::uint8_t
    {
        MainDataset,
        IndexDataset,
        PLY,
        LAM
    };

    /**
     * @brief   : 初始化表数据
     */
    void InitialDatasetInfo () override;

    /**
     * @brief   : 将缓存数据转储到文件内
     */
    void DumpWriteCahce () override;

    /**
     * @brief   : 加载文件数据至缓存
     * @param   : loadIndex
     */
    std::vector<DataType>* LoadReadCache (std::size_t index) override;

    /**
     * @brief   : 对数据进行排序
     */
    void SortData () override;

    /**
     * @brief    Get the All Data object
     *
     * @return
     * @author   wxc
     * @date     2024.12.25
     */
    std::vector<SG::DataStructure::FEM::PCOMPData> GetAllData () override;

    /**
     * @brief   : 将自己的数据转存为nastran的数据结构
     */
    void OwnToNastran (std::vector<SG::DataStructure::FEM::PCOMPData>&              own,
                       std::vector<PCOMP::IDENTITY>& _OUT                           IDENTITYData,
                       std::vector<SG::DataStructure::FEM::ShellSectionLayer>& _OUT PLYData,
                       std::vector<SG::DataStructure::Common::I32>& _OUT            LAMData,
                       bool                                                         isSort = false);

    /**
     * @brief   : 将NASTRAN的数据转存为自己的数据结构
     * @param   : IDENTITY
     * @param   : INFO
     * @param   : own
     */
    void NastranToOwn (std::vector<PCOMP::IDENTITY>&                           IDENTITYData,
                       std::vector<SG::DataStructure::FEM::ShellSectionLayer>& PLYData,
                       std::vector<SG::DataStructure::Common::I32>&            LAMData,
                       std::vector<SG::DataStructure::FEM::PCOMPData>& _OUT    own);
};
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
